package leetcode;

public class reverseLinked {

      //Definition for singly-linked list.
      //反转链表
      public class ListNode {
          int val;
          ListNode next;
          ListNode() {}
          ListNode(int val) { this.val = val; }
          ListNode(int val, ListNode next) { this.val = val; this.next = next; }
      }


    public ListNode reverseList(ListNode head) {
        if(head == null) {
            return head;
        }
        if(head.next == null) {
            return head;
        }
        ListNode cur = head.next;
        head.next = null;

        while(cur != null) {
            ListNode curNext = cur.next;//记录
            cur.next = head;//头插法
            head = cur;
            cur = curNext;
        }

        return head;
    }

}
